package com.dst.smart.handler.disposer;

import android.content.Context;
import android.util.Log;
import com.dst.smart.app.MainActivity;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.player.entity.SongPlayInfo;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 股票。
 * 今天的港股大盘
 科大讯飞的行情
 查询股票002230
 * 
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年9月5日 下午4:58:10
 *
 */
public class StockDisposer extends Disposer {
	private static final String KEY_URL = "url";

	public StockDisposer(Context context) {
		super(context);
	}
	
	@Override
	public void disposeResult(final SemanticResult semanticResult) {
		try {		if (semanticResult != null) {
			JSONArray result = semanticResult.getResult();
			if (result != null && result.length() != 0) {
				//MainActivity.playSound(5, semanticResult.getAnswerText());
				List<SongPlayInfo> playList = new ArrayList<SongPlayInfo>();
				SongPlayInfo songPlayInfo;
				if (semanticResult.getAnswerText().length() > 0) {
					songPlayInfo = new SongPlayInfo();
					songPlayInfo.setPlayUrl(semanticResult.getAnswerText());
					playList.add(songPlayInfo);
				}
				JSONObject jsonObject = result.getJSONObject(0);
				String stockCode = "股票代码." + jsonObject.getString("stockCode");
				String currentPrice = "当前价." + jsonObject.getString("currentPrice");
				String openingPrice = "开盘价." + jsonObject.getString("openingPrice");
				String highPrice = "最高价." + jsonObject.getString("highPrice");
				String lowPrice = "最低价." + jsonObject.getString("lowPrice");
				String riseRate = "上升率." + jsonObject.getString("riseRate");
				//String text=semanticResult.getAnswerText()+stockCode+currentPrice+openingPrice+highPrice+lowPrice+riseRate;
				songPlayInfo = new SongPlayInfo();
				songPlayInfo.setPlayUrl(stockCode);
				playList.add(songPlayInfo);
				songPlayInfo = new SongPlayInfo();
				songPlayInfo.setPlayUrl(currentPrice);
				playList.add(songPlayInfo);
				songPlayInfo = new SongPlayInfo();
				songPlayInfo.setPlayUrl(openingPrice);
				playList.add(songPlayInfo);
				songPlayInfo = new SongPlayInfo();
				songPlayInfo.setPlayUrl(highPrice);
				playList.add(songPlayInfo);
				songPlayInfo = new SongPlayInfo();
				songPlayInfo.setPlayUrl(lowPrice);
				playList.add(songPlayInfo);
				songPlayInfo = new SongPlayInfo();
				songPlayInfo.setPlayUrl(riseRate);
				playList.add(songPlayInfo);
				//getPlayController().playURLList(semanticResult.getUUID(),null);
				//getPlayController().playText(semanticResult.getUUID(),text , true, "", null);
				getPlayController().playURLList(semanticResult.getUUID(), playList);
				getPlayController().playText(semanticResult.getUUID(), playList.get(0).getPlayUrl(), true, "", null);

				//sendAiuiData("股票","stock",result.toString());
			} else {
				if (semanticResult.getAnswerText().length() > 0) {
					getPlayController().playURLList(semanticResult.getUUID(), null);
					getPlayController().playText(semanticResult.getUUID(), semanticResult.getAnswerText(), true, "", null);
				}
			}
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.STOCK);
	}
}
